package com.glc.hms.common.util;

import java.util.Random;

/**
 * 获取随机字符串工具类
 *
 * @author ljt
 * @since 2019-11-27 23:46
 */
public class RandomUtils {
    private static Random random = new Random();
    private static final String CHARS ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String LOWERS ="abcdefghijklmnopqrstuvwxyz";
    private static final String UPPERS ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBERS ="0123456789";

    /**
     * 生成随机的字符串，取值：a-z A-Z 0-9 的组合
     * @param length 要生成的字符串长度
     * @return 随机字符串
     */
    public static String randomString(int length){
        StringBuilder result = new StringBuilder(length);
        for (int i = 0 ; i < length; i++){
            result.append(CHARS.charAt(random.nextInt(CHARS.length())));
        }
        return result.toString();
    }


    /**
     * 生成随机的纯小写字母字符串，取值：a-z
     * @param length 要生成的字符串长度
     * @return 纯小写字母字符串
     */
    public static String randomLowerString(int length){
        StringBuilder result = new StringBuilder(length);
        for (int i = 0 ; i < length; i++){
            result.append(LOWERS.charAt(random.nextInt(LOWERS.length())));
        }
        return result.toString();
    }

    /**
     * 生成随机的纯大写字母字符串，取值：A-Z
     * @param length 要生成的字符串长度
     * @return 纯大写字母字符串
     */
    public static String randomUpperString(int length){
        StringBuilder result = new StringBuilder(length);
        for (int i = 0 ; i < length; i++){
            result.append(UPPERS.charAt(random.nextInt(UPPERS.length())));
        }
        return result.toString();
    }

    /**
     * 生成随机的纯数字字符串，取值：0-9
     * @param length 要生成的字符串长度
     * @return 纯数字字符串
     */
    public static String randomIntString(int length){
        StringBuilder result = new StringBuilder(length);
        for (int i = 0 ; i < length; i++){
            result.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
        }
        return result.toString();
    }

    /**
     * 随机生成一个指定范围的整数 [0,range)
     * @param range
     * @return
     */
    public static int randomInt(int range){
        return random.nextInt(range);
    }
}
